package com.xxx.test;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.LinkedList;

/**
 * @author hdz
 * @date 2019/03/29
 */
public class AppInfoAdapter extends BaseAdapter {

    private LinkedList<AppInfo> mAppInfoData;
    private Context mContext;

    public AppInfoAdapter(LinkedList<AppInfo> data, Context context) {
        this.mAppInfoData = data;
        this.mContext = context;
    }

    @Override
    public int getCount() {
        return mAppInfoData.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if(convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.app_info_item,parent,false);
            holder = new ViewHolder();
            holder.imgLogo = (ImageView) convertView.findViewById(R.id.item_logo);
            holder.txtName = (TextView) convertView.findViewById(R.id.item_name);
            holder.txtPkgName = (TextView) convertView.findViewById(R.id.item_pkg_name);
            //将Holder存储到convertView中
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        holder.imgLogo.setImageDrawable(mAppInfoData.get(position).getLogo());
        holder.txtName.setText(mAppInfoData.get(position).getName());
        holder.txtPkgName.setText(mAppInfoData.get(position).getPackageName());
        return convertView;
    }

    static class ViewHolder{
        ImageView imgLogo;
        TextView txtName;
        TextView txtPkgName;
    }
}
